{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f870c290-bf69-4fb5-8df7-2d4013f50d1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16d008d2-51e5-48a4-82e2-b68bb4989401",
   "metadata": {},
   "source": [
    "创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a4a679c3-c666-4fa1-87d2-5155d135eb2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,2],[3,4]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a28b5bd9-2fdc-4713-b8a7-b49fa3146b27",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]]\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3849e927-901d-4ab7-abf2-9d3248d7b998",
   "metadata": {},
   "source": [
    "创建矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dfdece81-ec19-4dfa-a63e-1fa4c2a40eba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2],\n",
       "        [3, 4]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.mat([[1,2],[3,4]])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2dae5f2e-fc55-4138-b7ff-f40d2e100066",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]]\n"
     ]
    }
   ],
   "source": [
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a7119df4-dd17-4c20-83f7-0b3c0a1a4fc7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2],\n",
       "        [3, 4]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mat('1 2;3 4')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b0ed9ec4-3e4d-478b-b961-ba95bcadd2a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2],\n",
       "        [3, 4]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matrix([[1,2],[3,4]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9840d702-9eee-4cc7-92ec-b1e567f73801",
   "metadata": {},
   "source": [
    "矩阵拼接-bmat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "63f40113-19b8-4c92-b265-d4923dcd13f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,2],[3,4]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3c6bbf99-2e02-46b8-b4ea-ab5b1f9303cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[11, 12],\n",
       "       [13, 14]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([[11,12],[13,14]])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6ed21695-a771-48e7-818e-3f744cc88b3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 1,  2, 11, 12],\n",
       "        [ 3,  4, 13, 14]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.bmat('a b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cbbc3e2d-12e6-40b4-b264-7221fe7d0b9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 1,  2, 11, 12],\n",
       "        [ 3,  4, 13, 14],\n",
       "        [11, 12,  1,  2],\n",
       "        [13, 14,  3,  4]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.bmat('a b;b a')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d39ade1-2fdd-4366-a15c-bdf33ff0ad61",
   "metadata": {},
   "source": [
    "矩阵运算\n",
    "https://blog.csdn.net/li2008kui/article/details/107128994"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "63b6d968-7767-4524-8c49-96f269fd490d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2],\n",
       "        [3, 4]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.mat([[1,2],[3,4]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e35e5c3d-89e9-4b1c-bff4-11d4e99efafe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 1],\n",
       "        [0, 1]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b= np.mat([[1,1],[0,1]])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a5b62ded-7c17-4aa5-a8ef-8ea6ed1f0622",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[2, 4],\n",
       "        [6, 8]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = a*2 # 矩阵与数相乘\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "84f98113-3f31-4b82-b978-aa5fc1d55af1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 3],\n",
       "        [3, 7]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# d = a+b\n",
    "# d = a-b\n",
    "# 矩阵相称，对应行与列相乘之后在相加\n",
    "# 1*1 + 2*0 = 1\n",
    "# 1*1 + 2*1 = 3\n",
    "# 3*1 + 4*0 = 3\n",
    "# 3*1 + 4*1 = 7\n",
    "d =  a * b \n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a68ebed2-7b49-412d-8f2d-3293abe11421",
   "metadata": {},
   "source": [
    "矩阵特有属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b3aee620-dd63-4edb-af39-dccbb188570c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 0],\n",
       "        [1, 1]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matrix(b).T # 转置矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8cb9941c-c506-477c-a16b-5f18fedd614c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 0],\n",
       "        [1, 1]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "f06046e8-c879-4eba-b7df-c6b6f24fbfe1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 0],\n",
       "        [1, 1]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.H # 共轭转置矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "24a7bdae-96ec-4087-b3c8-c2679583d625",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 1., -1.],\n",
       "        [ 0.,  1.]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.I # 逆矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "6d4edcf7-27f3-4763-bee3-2bf29fb878ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1],\n",
       "       [0, 1]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.A # 返回自身数据的二维数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e9c0731-ad25-40b3-b908-bec9f93decf8",
   "metadata": {},
   "source": [
    "ufunc 函数运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb047c93-d7ef-458a-8e77-edb774f0328f",
   "metadata": {},
   "source": [
    "* 数组的四则运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "c27762af-ecbb-4048-b97b-06f542580297",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1,2,3])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "f3d73a39-0a4a-4b37-92e0-020084f2f804",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 5, 6])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([4,5,6])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "b9457bc8-1c66-44a0-952a-f1821dcef30e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 7, 9])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "373c9610-13c5-46bc-8ab2-f2a4b2a294e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-3, -3, -3])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a-b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "4a443b62-5f35-4ec6-80c0-cbc215ccfc7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4, 10, 18])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a*b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "9f310999-ec73-4c43-968a-4cb1567cdcb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.25, 0.4 , 0.5 ])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a/b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09564f1d-a1ce-44af-9077-f5d9deb17361",
   "metadata": {},
   "source": [
    "* 数组的比较运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "f1a831de-26f2-40c7-93f1-61ce317b1769",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a>b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "aefa1a42-179a-4131-870b-1577cb2eaa53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a<=b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afefcc73-2d94-4632-8427-a0469d5f4de4",
   "metadata": {},
   "source": [
    "* 数组的逻辑运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "c461ffb9-63e8-4ea3-9bbf-1a4ef4a8dd9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.all(a==b) # 与"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "964e1b4c-4073-43cc-adfa-bfaf6288088b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.any(a==b) # 或"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd4c0878-e1b7-40a4-9e9b-02ac37282bd1",
   "metadata": {},
   "source": [
    "ufunc广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "a4da2317-b377-43fc-8e8c-f21d5e201db1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "7423d450-b6c1-4246-9aac-30ddc3b2ab5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 2])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([2,2,2])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "cb0a212a-6d5a-421c-b5c4-5419ffe1f0d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "7e8932e0-aa26-40ba-a289-a9cc770edc0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3,)"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "dec02ced-2f33-4abf-b726-ea5c76d48e5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3,  4,  5],\n",
       "       [ 6,  7,  8],\n",
       "       [ 9, 10, 11]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(a) + np.array(b) # b 的数组重复三次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "6a19cf96-3954-42d8-9f51-87dac1b780b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [1],\n",
       "       [1]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.array([[1],[1],[1]])\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "ba9a273f-73dd-4ba1-bd60-c6a9d99908f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  3,  4],\n",
       "       [ 5,  6,  7],\n",
       "       [ 8,  9, 10]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(a)+np.array(c) # c的每个元素重复3次与其相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6c164be-7e61-4526-88d3-7ed8e6616e6d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
